package com.prp.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.prp.service.ServiceFactory;

public class DownloadAction extends ActionSupport {

	private String filename;
	private String downloadFile;
	private int itemId; 
	/**
	 * @return
	 */
	public String execute() {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	public void setItemId(int itemId) {
		this.itemId = itemId;
	}
	public int getItemId() {
		return itemId;
	}
	
	public InputStream getTargetFile() throws IOException{
		
		String name = ServiceFactory.getFileUploadService().getDownFile(itemId);
		String realPath = ServletActionContext.getServletContext().getRealPath("");
		if (name == null)
			return null;
		
		
		String[] context = name.split("/");
		
		filename = context[context.length - 1];
		filename = new String(filename.getBytes("gbk"), "iso-8859-1");
		
		ServletActionContext.getResponse().setContentType("application/x-msdownload");
		FileInputStream stream = new FileInputStream(new File(realPath + name));
		
		return stream;
		
	}
	public String getDownloadFile()
	{
		return this.downloadFile;
	}
	public void setDownloadFile(String downloadFile) {
		this.downloadFile = downloadFile;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	public String getFilename() {
		return filename;
	}
}